all_docs_by_mention_single <- subset(
    all_docs,
    !is.na(user_mentions) & !grepl(",", user_mentions)
) %>%
    mutate(user_mentions_unlisted = unlist(user_mentions_unlisted))

## in warning message here new names are for the discarded list
all_docs_by_mention_multiple <- subset(
    all_docs,
    !is.na(user_mentions) & grepl(",", user_mentions)
) %>%
    unnest(user_mentions_unlisted)

all_docs_by_mention <- rbind(
    all_docs_by_mention_single,
    all_docs_by_mention_multiple
)

aggregated_outside_mention_tweets <- subset(
    all_docs_by_mention,
    not_holdout_alt
    & !is.na(user_mentions_unlisted)
    & !(user_mentions_unlisted %in% all_trolls)
) %>%
    mutate(
        tweet_date = lubridate::floor_date(as.Date(substr(tweet_time, 1, 10)),unit="week"),
        polarized_troll = as.integer(userid %in% c(left_trolls, right_trolls)),
        news_troll = as.integer(userid %in% news_trolls),
        commercial_troll = as.integer(userid %in% commercial_trolls),
        no_category_troll = as.integer(userid %in% no_category_trolls),
        fearmonger_troll = as.integer(userid %in% fearmonger_trolls),
        unknown_troll = as.integer(userid %in% unknown_trolls),
        hashtag_troll = as.integer(userid %in% hashtag_trolls)
    ) %>%
    group_by(tweet_date, polarized_troll, news_troll, no_category_troll, hashtag_troll, commercial_troll, fearmonger_troll, unknown_troll) %>%
    summarise(
        outside_tweets = n()
    )

aggregated_outside_mention_prop <- subset(
    all_docs_by_mention,
    not_holdout_alt
    & !is.na(user_mentions_unlisted)
    & !(user_mentions_unlisted %in% all_trolls)
    & userid %in% all_trolls
) %>%
    mutate(
        tweet_date = lubridate::floor_date(as.Date(substr(tweet_time, 1, 10)),unit="week")
    ) %>%
    group_by(tweet_date) %>%
    summarise(
        polarized_mentions = mean(as.integer((
            userid %in% c(left_trolls, right_trolls)
        )))
    )

count_tweets <- subset(
    all_docs,
    not_holdout_alt
) %>%
    mutate(
        tweet_date = lubridate::floor_date(as.Date(substr(tweet_time, 1, 10)),unit="week")
    ) %>%
    group_by(tweet_date) %>%
    summarise(
        polarized_tweets = sum(userid %in% c(left_trolls, right_trolls)),
        left_tweets = sum(userid %in% c(left_trolls)),
        right_tweets = sum(userid %in% c(right_trolls)),
        news_tweets = sum(userid %in% news_trolls),
        commercial_tweets = sum(userid %in% commercial_trolls),
        no_category_tweets = sum(userid %in% no_category_trolls),
        fearmonger_tweets = sum(userid %in% fearmonger_trolls),
        unknown_tweets = sum(userid %in% unknown_trolls),
        hashtag_tweets = sum(userid %in% hashtag_trolls)
    )

sum(count_tweets[count_tweets$tweet_date < "2014-06-01",-1])
sum(count_tweets[count_tweets$tweet_date >= "2014-06-01",-1])


pdf("figs/FigureA10_A11_troll_activity_replicate_psrm.pdf", width=8, height=3.5)
##
par(mar=c(4,6,3,1))
with(
    subset(aggregated_outside_mention_tweets, hashtag_troll==1),
    plot(
        as.Date(tweet_date), outside_tweets, type="l",
        col="green3",
        bty="n",
        xlab="",
        ylab="Number of Non-Troll Mentions",
        lwd=4, cex.lab=1.3, xaxt="n",
        ylim=c(0, max(aggregated_outside_mention_tweets$outside_tweets)),
        xlim = c(as.Date("2014-06-01"), as.Date("2016-12-01"))
    )
)
with(
    subset(aggregated_outside_mention_tweets, polarized_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="purple",
        lwd=4
    )
)
with(
    subset(aggregated_outside_mention_tweets, commercial_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="yellow3",
        lwd=4
    )
)
with(
    subset(aggregated_outside_mention_tweets, fearmonger_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="black",
        lwd=4
    )
)
with(
    subset(aggregated_outside_mention_tweets, unknown_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="gray",
        lwd=4
    )
)
with(
    subset(aggregated_outside_mention_tweets, no_category_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="pink",
        lwd=4
    )
)
with(
    subset(aggregated_outside_mention_tweets, news_troll==1),
    lines(
        as.Date(tweet_date), outside_tweets, type="l",
        col="orange",
        lwd=4
    )
)
axis(1, seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=NA)
text(y=-18000, x=seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=as.yearmon(seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter")), cex.axis=1, xpd=TRUE, srt=45)
legend(
    x=as.numeric(as.Date("2015-12-01")),
    y=70000,
    ## "topright",
    bty="n",
    legend=c("Left or right troll", "Hashtag gamer", "News aggregator", "Commercial","Fearmonger","Unknown", "No label -- not in LW"),
    cex=0.8,
    col=c("purple","green3","orange", "yellow3","black","gray","pink"), lty=1, lwd=3,
    title="\nCategories:\nLinvill & Warren (2018)", xpd=T
)
## ##
par(mar=c(4,6,3,1))
with(
    aggregated_outside_mention_prop,
    plot(
        as.Date(tweet_date), polarized_mentions, type="l",
        col="purple",
        bty="n",
        xlab="",
        ylab="Fraction of non-troll mentions\n coming from filter bubbles",
        lwd=4, cex.lab=1.3, xaxt="n",
        ylim=c(0, 1),
        xlim = c(as.Date("2014-06-01"), as.Date("2016-12-01"))
    )
)
axis(1, seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=NA)
text(y=-0.25, x=seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=as.yearmon(seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter")), cex.axis=1, xpd=TRUE, srt=45)
abline(v = as.Date("2015-08-03"), col = "black", lwd = 4, lty = 3)
mtext("First Republican\nPresidential Debate", side = 3, cex=0.9, at = as.Date("2015-08-03"))
##
par(mar=c(4,6,3,1))
with(
    count_tweets,
    plot(
        as.Date(tweet_date), hashtag_tweets, type="l",
        col="green3",
        bty="n",
        xlab="",
        ylab="Number of Tweets",
        lwd=4, cex.lab=1.3, xaxt="n",
        ylim=c(0, max(count_tweets$polarized_tweets)),
        xlim = c(as.Date("2014-06-01"), as.Date("2016-12-01"))
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), polarized_tweets, type="l",
        col="purple",
        lwd=4
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), commercial_tweets, type="l",
        col="yellow3",
        lwd=4
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), news_tweets, type="l",
        col="orange",
        lwd=4
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), unknown_tweets, type="l",
        col="gray",
        lwd=4
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), fearmonger_tweets, type="l",
        col="black",
        lwd=4
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), no_category_tweets, type="l",
        col="pink",
        lwd=4
    )
)
axis(1, seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=NA)
text(y=-8000, x=seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=as.yearmon(seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter")), cex.axis=1, xpd=TRUE, srt=45)
##
legend(
    x=as.numeric(as.Date("2015-11-01")), y=34000, bty="n",
    legend=c("Left or right troll", "Hashtag gamer", "News aggregator", "Commercial","Fearmonger","Unknown","No label -- not in LW"),
    cex=0.8,
    col=c("purple","green3","orange", "yellow3","black", "gray","pink"), lty=1, lwd=3,
    title="\nCategories:\nLinvill & Warren (2018)", xpd=T
)
par(mar=c(5,6,3,1))
with(
    count_tweets,
    plot(
        as.Date(tweet_date), right_tweets, type="l",
        col="red",
        bty="n",
        xlab="",
        ylab="Number of Tweets",
        lwd=4, cex.lab=1.3, xaxt="n",
        ylim=c(0, max(count_tweets$left_tweets)),
        xlim = c(as.Date("2014-06-01"), as.Date("2016-12-01"))
    )
)
with(
    count_tweets,
    lines(
        as.Date(tweet_date), left_tweets, type="l",
        col="blue",
        lwd=4
    )
)
axis(1, seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=NA)
text(y=-8000, x=seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter"), labels=as.yearmon(seq(as.Date("2014-06-01"),as.Date("2016-11-01"), by="quarter")), cex.axis=1, xpd=TRUE, srt=45)
##
legend(
    x=as.numeric(as.Date("2015-11-01")), y=20000, bty="n",
    legend=c("Left troll", "Right troll"),
    cex=0.8,
    col=c("blue","red"), lty=1, lwd=3,
    title="\nCategories:\nLinvill & Warren (2018)", xpd=T
)
abline(v = as.Date("2015-08-03"), col = "black", lwd = 4, lty = 3)
mtext("First Republican\nPresidential Debate", side = 3, cex=0.9, at = as.Date("2015-08-03"))
dev.off()



